namespace SampleApp.Controllers
{
    using System.Collections.Generic;
    using System.Data;
    using System.Data.Entity;
    using System.Linq;
    using System.Web.Mvc;

    using SampleApp.Models;

    public class CartItemsController : Controller
    {
        private SampleAppContext context = new SampleAppContext();

        //
        // GET: /CartItems/

        public ViewResult Index()
        {
            return View(context.CartItems.Include(cartitem => cartitem.Product).ToList());
        }

        //
        // GET: /CartItems/Details/5

        public ViewResult Details(int id)
        {
            CartItem cartitem = context.CartItems.Single(x => x.Id == id);
            return View(cartitem);
        }

        //
        // GET: /CartItems/Create

        public ActionResult Create()
        {
            ViewBag.PossibleCategories = context.Categories;
            ViewBag.PossibleProducts = context.Products;
            return View();
        } 

        //
        // POST: /CartItems/Create

        [HttpPost]
        public ActionResult Create(CartItem cartitem)
        {
            if (ModelState.IsValid)
            {
                context.CartItems.Add(cartitem);
                context.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.PossibleCategories = context.Categories;
            ViewBag.PossibleProducts = context.Products;
            return View(cartitem);
        }
        
        [HttpPost]
        public ActionResult CreateCart(IEnumerable<CartItem> cartItems) {
            if(ModelState.IsValid) {
                foreach (var cartItem in cartItems) {
                    context.CartItems.Add(cartItem);
                }
                context.SaveChanges();
                return Json("Cart created");
            } else {
                return Json("Validation failed");
            }
        }

        //
        // GET: /CartItems/Edit/5
 
        public ActionResult Edit(int id)
        {
            CartItem cartitem = context.CartItems.Single(x => x.Id == id);
            ViewBag.PossibleCategories = context.Categories;
            ViewBag.PossibleProducts = context.Products;
            return View(cartitem);
        }

        //
        // POST: /CartItems/Edit/5

        [HttpPost]
        public ActionResult Edit(CartItem cartitem)
        {
            if (ModelState.IsValid)
            {
                context.Entry(cartitem).State = EntityState.Modified;
                context.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.PossibleCategories = context.Categories;
            ViewBag.PossibleProducts = context.Products;
            return View(cartitem);
        }

        //
        // GET: /CartItems/Delete/5
 
        public ActionResult Delete(int id)
        {
            CartItem cartitem = context.CartItems.Single(x => x.Id == id);
            return View(cartitem);
        }

        //
        // POST: /CartItems/Delete/5

        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {
            CartItem cartitem = context.CartItems.Single(x => x.Id == id);
            context.CartItems.Remove(cartitem);
            context.SaveChanges();
            return RedirectToAction("Index");
        }
    }
}